

import {createRouter,createWebHistory} from 'vue-router'
import HomeView from '../views/HomeView.vue'
import Layout from '../layout/IndexView.vue'
import store from '../store'

// 从基础路由：不需要权限
export const baseRoute = [{
    path: '/',
    component: Layout,
    meta: {
      title: '首页',
      icon: 'home',
      isShowOne: true
    }, // isShowOne是否显示为顶级栏目
    children: [{
      path: '',
      name: 'home',
      component: HomeView,
      // meta:{title:'首页',icon:'home',isShowOne:true}, // 顶级栏目的meta不写在children里面
    }]
  },
  {
    path: '/401',
    component: () => import('@/views/401'),
    hidden: true
  }, {
    path: '/404',
    component: () => import('@/views/404'),
    meta: {
      title: '404'
    },
    hidden: true
  }
]
// 需要权限的路由，meta.title设置导航标题，meta.role为所需权限
export const asyncRoutes = [{
    path: '/permission',
    component: Layout,
    meta: {
      title: '权限管理',
      role: ['210', '220'],
      icon: '1',
      isShowOne: true
    }, //role:包含children的所有role
    children: [{
      path: 'index',
      name: '权限列表',
      meta: {
        title: '权限列表',
        role: ['210'],
        icon: '1'
      },
      component: () => import('../views/permission/IndexView.vue')
    }, {
      path: 'add',
      name: '添加权限',
      meta: {
        title: '添加权限',
        role: ['220'],
        icon: '1'
      },
      component: () => import('../views/permission/addView.vue')
    }]
  }, {
    path: '/nested',
    component: Layout,
    meta: {
      title: '路由嵌套',
      role: ['310', '320', '330', '331', '332'],
      icon: '2'
    },
    children: [{
      path: 'menu1',
      meta: {
        title: '菜单1',
        role: ['310'],
        icon: '2'
      },
      component: () => import('../views/nestedRouter/menu-1.vue')
    }, {
      path: 'menu2',
      meta: {
        title: '菜单2',
        role: ['320'],
        icon: '2'
      },
      component: () => import('../views/nestedRouter/menu-2.vue')
    }, {
      path: 'menu3',
      meta: {
        title: '菜单3',
        role: ['331', '332'],
        icon: '2'
      },
      redirect: '/nested/menu3/menu31',
      component: () => import('../views/nestedRouter/menu-3/menu-3.vue'), //父组件
      children: [{
        path: 'menu31',
        meta: {
          title: '菜单3-1',
          role: '331',
          icon: '2'
        },
        component: () => import('../views/nestedRouter/menu-3/menu-3-1.vue'),
      }, {
        path: 'menu32',
        meta: {
          title: '菜单3-2',
          role: ['332'],
          icon: '2'
        },
        component: () => import('../views/nestedRouter/menu-3/menu-3-2.vue'),
      }]
    }]
  }, {
    path: '/about',
    component: Layout,
    meta: {
      title: '关于',
      role: ['500'],
      icon: '1',
      // icon: 'https://tse1-mm.cn.bing.net/th/id/R-C.e097f21ab557788dd46e636a336456eb?rik=k6OeLIQO%2fL1ByQ&riu=http%3a%2f%2fcdn.onlinewebfonts.com%2fsvg%2fimg_200865.png&ehk=RoNIcV%2bapKyiUka3Clxtqmeiv%2bw%2fU8qiAeLyJLBP5rs%3d&risl=&pid=ImgRaw&r=0',
      isShowOne: true
    },
    children: [{
      path: '',
      name: '关于',
      component: () => import('../views/AboutView.vue'),
      // meta:{title:'关于', role:['500'],icon:'3',isShowOne:true}
    }]
    // },{
    //   path: '*',
    //   redirect:'/404',
    //   hidden:true
}]
const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes: baseRoute
})

// const router = myRouter

export default router

/**
 * 添加动态路由函数（把添加路由addRoute返回的函数存储在store用于移除路由）
 * @param {array} routes 筛选出来的需要添加的路由数组
 */
export function addAsyncRouter(routes){
  let removeRouteFuns = []
  routes.forEach(item => {
    let removeRouterfun = router.addRoute(item) 
    removeRouteFuns.push(removeRouterfun)
  });
  store.commit('permission/SET_ASYNCROUTES',removeRouteFuns)

}
/**
 * 移除动态路由函数
 */
export function resetAsyncRouter(){
  store.getters.asyncRoutes.forEach(fn => {
    fn()
  });
}